$(function() {
	var teamId='';
	if(window.localStorage.getItem("backstage-access-teamId") == 'undefined'){
		teamId = '';
	}else{
		teamId = window.localStorage.getItem("backstage-access-teamId");
	}
	$('#userList').getList({
		"initData": {
			"teamId": teamId=="undefined"?'':teamId,
			"num": 1,
			"size": 10
		},
		"url": site.apiUrl + "/sUsers/selectUsersList", // 数据请求url
		"type": "get",
		"ajaxform": true,
		"checkname": "datachk", //选择器名称，为空不显示选择器
		"col": [ //数据列
			{
				"field": "nickName",
				"display": "名称",
				"width": "1.5"
			},
			{

				"field": "assumeOfficeDisplay",
				"display": "职位",
				"width": "1.5"
			},
			{
				"field": "sexDisplay",
				"display": "性别",
				"width": "1.5"
			},
			{
				"field": "identityCard",
				"display": "身份证",
				"width": "1.5"
			},
			{
				"field": "phone",
				"display": "手机号",
				"width": "1.5"
			},
			{
				"field": "emailAddress",
				"display": "邮箱",
				"width": "1.5"
			},
			{
				"field": "isUsingDisplay",
				"display": "是否启用",
				"width": "1.5"
			},
			{
				"field": "createTime",
				"display": "创建时间"
			}
		],
		"option": [ //操作按钮配置
			/*{"icon":"view","tip":"查看","fun":"brandView('{{id}}')"},  //icon: view,edit,add,del*/
			{
				"icon": "view",
				"tip":'查看',
				"list":[
					{"display":'查看任务',"fun": "taskView('{{id}}')"},
					{"display":'考勤记录',"fun": "workView('{{id}}')"}
				]
			}, 
			{
				"icon": "reset",
				"tip": "重置密码",
				"fun": "pesetPwd('{{id}}')"
			},
			{
				"icon": "edit",
				"tip": "编辑",
				"fun": "brandEdit('{{id}}')"
			}, // 花括号内可以对应相关字段
			{
				"icon": "del",
				"tip": "删除",
				"fun": "deleteBrand('{{id}}')"
			}
		]
	});
	departmentList()
	//导入
	//	$("#getexcel").chag(function(){
	//		 console.log($(this).val());
	//	});

	$("#getexcel").on("input", function(e) {
		//判断是不是要求的上传类型
		if(!/\.(xls|xlsx|XLS|XLSX)$/.test($(this).val())) {
			ly.toast("导入文件格式不正确");
			$(this).val("");
			return false;
		}
		
		var formData = new FormData();
		formData.append('file', e.target.files[0]);
		$.ajax({
			headers: {
				"access-token": localStorage.getItem("backstage-access-token")
			},
			url: site.gateUrl + '/usersManagement/importColl',
			type: "post",
			data: formData,
			processData: false, // 告诉jQuery不要去处理发送的数据  
			contentType: false, // 告诉jQuery不要去设置Content-Type请求头  
			success: function(r) {
				if(r.code == "rest.success") {
					if(!r.result){
						ly.alert('导入失败',function(){
						
						});
					}else{
						ly.toast('导入成功');
						$("#userList").getList.refresh();//刷新当前分页
					}
				} else {
					ly.toast(r.desc);
					//延迟2.5秒后刷新
					setTimeout(function () {
						window.location.reload();
					},2500)

				}
			},
			error: function(e) {
				ly.toast("服务出错");
			}

		});

	});
	
	//批量删除
	$("#batchDelBtn").click(function(){
		var c = "";
		var ids = []
		if($("input[name=datachk]:checked").length > 0){
			$("input[name=datachk]:checked").each(function(){
				ids.push($(this).val())
			});
		}
		// console.log(ids.join(','))
		c = ids.join(',')
		
		if(c!= null && c!=''){
			deleteBrand(c);
		}else{
			ly.toast("请选择删除项");
		}
		
	});

})
//编辑
function brandEdit(id) {
	ly.goto("add-user.html?id=" + id)
}
//查看任务
function taskView(id) {
	ly.goto("task-list.html?id=" + id)
}

//考勤记录
function workView(id){
	ly.goto("user-attendance.html?id=" + id)
}

function departmentList(){
	var urls =''+site.gateUrl+'/back/team/selectTeamList'
	request({
		url:urls ,
		data:{
			"num":1,
			"size":30,
		},
		ajaxform:true,
		type: 'get',
		callback: function(r) {
			 console.log(r)
			 
			if(r.code=="rest.success"){
				var lists = r.result.list;
				 var htmls = '';
				 for (let i = 0; i < lists.length; i++) {
						htmls+='<option value="'+lists[i].id+'">'+lists[i].teamName+'</option>'
				 }
				 $('#departmentList').append(htmls)
			}else{
			
			}
			// ly.toast('密码重置成功');
			// ly.toast(r.msg);
		}
	});
}


//重置密码
function pesetPwd(id) {
	ly.confirm("确认重置密码？",function(){
		request({
	        url: site.gateUrl + '/usersManagement/resetPassword',
	        data:{
				"uid":id
			},
	        ajaxform:true,
	        type: 'get',
	        callback: function(r) {
				if(r.code=="rest.success"){
					ly.toast('密码重置成功');
	        	}else{
	        		ly.toast(r.msg);
	        	}
	        }
		});
	});

}

//删除用户
function deleteBrand(id){
	ly.confirm("确认删除该用户？",function(){
		request({
	        url: site.gateUrl + '/usersManagement/deleteUsersById',
	        data:{
				"uid":id
			},
	        ajaxform:true,
	        type: 'get',
	        callback: function(r) {
				if(r.code=="rest.success"){
					ly.toast('删除成功');
					$("#userList").getList.refresh();//刷新当前分页
	        	}else{
	        		ly.toast(r.msg);
	        	}
	        }
		});
	});
}
